﻿/*
 * Created by SharpDevelop.
 * User: Guy Fomi
 * Date: 2/6/2011
 * Time: 2:14 PM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Collections;
using System.Collections.Generic;
using HtmlAgilityPack;
using System.Web.Script.Serialization;

namespace TomatoSynchronizer.Store
{
	/// <summary>
	/// Description of LanguageStore.
	/// </summary>
	public class LanguageStore
	{
		private Site _site;
		private HtmlWeb _client;
		
		public LanguageStore(Site site,HtmlWeb client)
		{
			_site = site;
			_client = client;
		}
		
		private IDictionary GetLanguage(string LanguageId)
		{
			List<IDictionary> entries = new List<IDictionary>();
			string url = _site.Url + "/admin/json.php?action=load_language&languages_id=" + LanguageId + "&module=languages";
			
			HtmlDocument doc = _client.Load(url);
			HtmlNode node = doc.DocumentNode.SelectSingleNode("/");
			
			JavaScriptSerializer serialiser = new JavaScriptSerializer();
			IDictionary json = serialiser.Deserialize<IDictionary>(node.InnerText);
			foreach(string key in json.Keys)
			{
				if(key == "data")
				{
					IDictionary Language = (IDictionary)json[key];
					entries.Add(Language);
				}
			}
			
			return entries[0];
		}
		
		public List<IDictionary> GetLanguages()
		{
			List<IDictionary> Languages = new List<IDictionary>();
			
			HtmlDocument doc = _client.Load(_site.Url + "/admin/json.php?action=list_languages&module=languages");
			HtmlNode node = doc.DocumentNode.SelectSingleNode("/");
			
			JavaScriptSerializer serialiser = new JavaScriptSerializer();
			IDictionary json = serialiser.Deserialize<IDictionary>(node.InnerText);
			foreach(string key in json.Keys)
			{
				if(key == "records")
				{
					object[] recs = (object[])json[key];
					for(int i = 0;i < recs.Length;i++)
					{
						IDictionary record = (IDictionary)recs[i];
						IDictionary Language = GetLanguage(record["languages_id"].ToString());
						Language["parent_language"] = GetParenLanguage(record["languages_id"].ToString());
						Language["translation_groups"] = GetTranslationGroups(record["languages_id"].ToString());
						Languages.Add(Language);
					}
				}
			}
			
			return Languages;
		}
		
		private List<IDictionary> GetTranslationGroups(string LanguageId)
		{
			List<IDictionary> TranslationGroups = new List<IDictionary>();
			string url = _site.Url + "/admin/json.php?action=list_translation_groups&languages_id=" + LanguageId + "&module=languages&node=root";
			
			HtmlDocument doc = _client.Load(url);
			HtmlNode node = doc.DocumentNode.SelectSingleNode("/");
			
			JavaScriptSerializer serialiser = new JavaScriptSerializer();
			string response = "{records:" + node.InnerText + "}";
			IDictionary json = serialiser.Deserialize<IDictionary>(response);
			foreach(string key in json.Keys)
			{
				if(key == "records")
				{
					object[] recs = (object[])json[key];
					for(int i = 0;i < recs.Length;i++)
					{
						IDictionary TranslationGroup = (IDictionary)recs[i];
						TranslationGroup["translations"] = GetTranslations(TranslationGroup["id"].ToString(),LanguageId);
						TranslationGroups.Add(TranslationGroup);
					}
				}
			}
			
			return TranslationGroups;
		}

		private List<IDictionary> GetTranslations(string TranslationGroup,string LanguageId)
		{
			List<IDictionary> Translations = new List<IDictionary>();
			string url = _site.Url + "/admin/json.php?action=list_translations&group=" + TranslationGroup + "&languages_id=" + LanguageId + "&module=languages";
			
			HtmlDocument doc = _client.Load(url);
			HtmlNode node = doc.DocumentNode.SelectSingleNode("/");
			
			JavaScriptSerializer serialiser = new JavaScriptSerializer();
			string response = "{records:" + node.InnerText + "}";
			IDictionary json = serialiser.Deserialize<IDictionary>(response);
			foreach(string key in json.Keys)
			{
				if(key == "records")
				{
					IDictionary json1 = (IDictionary)json[key];
					foreach(string cle in json1.Keys)
					{
						if(cle == "records")
						{
							object[] recs = (object[])json1[cle];
							for(int i = 0;i < recs.Length;i++)
							{
								IDictionary Translation = (IDictionary)recs[i];
								Translations.Add(Translation);
							}
						}
					}
				}
			}
			
			return Translations;
		}
		
		private List<IDictionary> GetParenLanguage(string LanguageId)
		{
			List<IDictionary> entries = new List<IDictionary>();
			string url = _site.Url + "/admin/json.php?action=get_parent_language&languages_id=" + LanguageId + "&module=languages";
			
			HtmlDocument doc = _client.Load(url);
			HtmlNode node = doc.DocumentNode.SelectSingleNode("/");
			
			JavaScriptSerializer serialiser = new JavaScriptSerializer();
			IDictionary json = serialiser.Deserialize<IDictionary>(node.InnerText);
			foreach(string key in json.Keys)
			{
				if(key == "data")
				{
					IDictionary Language = (IDictionary)json[key];
					entries.Add(Language);
				}
				
				if(key == "records")
				{
					object[] recs = (object[])json[key];
					for(int i = 0;i < recs.Length;i++)
					{
						IDictionary Language = (IDictionary)recs[i];
						entries.Add(Language);
					}
				}
			}
			
			return entries;
		}
	}
}
